﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace Project
{
    public partial class Customer : Form
    {
        static SqlConnection con;
        static SqlCommand cmd;
        static SqlDataReader dr;
        public Customer()
        {
            InitializeComponent();
            con = new SqlConnection();
            con.ConnectionString = @"server=localhost\sqlexpress;database=TSQLFundamentals2008;Integrated Security=true";
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "SELECT Max(custid) FROM Sales.Customers" ;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtcusID.Text = dr[0].ToString();
            }
      
        }

        bool valid()
        {
            if (txtCompanyName.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtCompanyName, "Enter Company Name, please!");
                txtCompanyName.Focus();
                return false;
            }

            if (txtContactName.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtContactName, "Enter Contact Name, please!");
                txtContactName.Focus();
                return false;
            }

            if (txtContactTitle.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtContactTitle, "Enter Contact Title, please!");
                txtContactTitle.Focus();
                return false;
            }

            if (txtAddress.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtAddress, "Enter Adddress, please!");
                txtAddress.Focus();
                return false;
            }

            if (txtCity.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtCity, "Enter City Name, please!");
                txtCity.Focus();
                return false;
            }

            if (txtRegion.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtRegion, "Enter Region Name, please!");
                txtRegion.Focus();
                return false;
            }

            if (txtPostalCode.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtPostalCode, "Enter Postal Code, please!");
                txtPostalCode.Focus();
                return false;
            }

            if (txtCountry.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtCountry, "Enter Country Name, please!");
                txtCountry.Focus();
                return false;
            }

            if (txtPhone.Text == "" )
            {
                errorProvider1.SetError(txtPhone, "Enter Phone Number, please!");
                txtPhone.Focus();
                return false;
            }

            if (txtFax.Text == "" )
            {
                errorProvider1.SetError(txtFax, "Enter Fax Number, please!");
                txtFax.Focus();
                return false;
            }
            errorProvider1.Clear();
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
           this.Close();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if(!valid()) return;
            MessageBox.Show("Customer ID: " + txtcusID.Text 
                             + "\n" + "Company name:  " + txtCompanyName.Text
                             + "\n" + "Contact name:  " + txtContactName.Text
                             + "\n" + "Contact title:  " + txtContactTitle.Text
                             + "\n" + "Address:  " + txtAddress.Text
                             + "\n" + "City:  " + txtCity.Text
                             + "\n" + "Region:  " + txtRegion.Text
                             + "\n" + "Postal Code:  " + txtPostalCode.Text
                             + "\n" + "Country:  " + txtCountry.Text
                             + "\n" + "Phone:  " + txtPhone.Text
                             + "\n" + "Fax:  " + txtFax.Text);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string companyname = txtCompanyName.Text;
            string contactname = txtContactName.Text;
            string contacttitle = txtContactTitle.Text;
            string address = txtAddress.Text;
            string city = txtCity.Text;
            string region = txtRegion.Text;
            string postalcode = txtPostalCode.Text;
            string country = txtCountry.Text;
            string phone = txtPhone.Text;
            string fax = txtFax.Text;
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "InsertCustomers";

            SqlParameter p = new SqlParameter("@companyname", companyname);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@contactname", contactname);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@contacttitle", contacttitle);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@address", address);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@city", city);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@region", region);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@postalcode", postalcode);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@country", country);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@phone", phone);
            cmd.Parameters.Add(p);
            p = new SqlParameter("@fax", fax);
            cmd.Parameters.Add(p);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
